<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>出库申请审核</title>
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="../../css/bootstrap-table.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="../../js/bootstrap-table.min.js"></script>
    <script src="../../js/bootstrap.min.js"></script>
    <script src="../../js/bootstrap-table-zh-CN.min.js"></script>
    <script>
        $(function () {
            $("#GatherTab").bootstrapTable({
                method: "get",
                contentType: "application/json;charset=utf-8",
                url: "/spay/queryspayToAudit",
                striped: true,
                pageNumber: 1,
                pagination: true,
                sidePagination: 'server',
                pageSize: 5,
                pageList: [3, 5, 6, 10],
                queryParams: function (params) {
                    return {
                        // 计算页号(当前页)
                        pageNumber: (params.offset / params.limit) + 1,
                        // 计算页大小
                        pageSize: params.limit,
                    }
                },
                columns: [
                    {title: '序号', field: 'id', align: 'center'},
                    {title: '申请单编号', field: 'payId', align: 'center'},
                    {title: '出库理由', field: 'reason', align: 'center', formatter: function (value, row, index) {

                            if (value == "c002-1") {
                                return "生产领料";

                            } else if (value == "c002-2") {

                                return "赠送";

                            } else if (value == "c002-3") {
                                return "内部借领";

                            } else if (value == "c002-4") {

                                return "其他借领"
                            } else {
                                return "其他";

                            }

                        }
                    },
                    {title: '总件数', field: 'amountSum', align: 'center'},
                    {title: '总金额', field: 'costPriceSum', align: 'center'},
                    {
                        title: '审核', field: 'id', align: 'center',
                        formatter: function (value, row, index) {
                            // console.log("--------"+JSON.stringify(row))
                            var btn1 = "<button  class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal2' onclick='a("+JSON.stringify(row)+")'>审核</button>";
                            return btn1;
                        }
                    },
                ]
            })
        });

        function findGatherDetailsById(id) {
            $.ajax({
                type: 'post',
                url: '/spay/findSpayDetailsById',
                data: 'id=' + id,
                success: function (result) {
                    console.log(result);
                    $("#tbody").html("");
                    $.each(result, function (index, val) {
                        // let total = parseInt(val.amount) * parseInt(val.costPrice)
                        $("#tbody").append("<tr>" +
                            "<td class='name'>" + val.id + "</td>" +
                            "<td class='name'>" + val.product_name + "</td>" +
                            "<td class='id'>" + val.product_id + "</td>" +
                            "<td class='number3'>" + val.number + "</td>" +
                            "<td class='amount'>" + val.amount + "</td>" +
                            "<td class='dw'>" + val.amount_unit + "</td>" +
                            "<td class='unit'>" + val.cost_price + "</td>" +
                            "<td>" + val.subtotal + "</td>" +
                            "</tr>");
                    })
                }
            })
        }

        function a(data) {
            findAcc();
            findGatherDetailsById(data.id);
            $("#check_time").val(new Date().toLocaleString())
            $("#storer").val(data.storer);
            var re='';
            if (data.reason == "c002-1") {
                re= "生产领料";
            } else if (data.reason == "c002-2") {
                re= "赠送";
            } else if (data.reason == "c002-3") {
                re= "内部借领";
            } else if (data.reason == "c002-4") {
                re= "其他借领"
            } else {
                re= "其他";
            }
            $("#reason").val(re);
            $("#amount_sum").val(data.amountSum);
            $("#cost_price_sum").val(data.costPriceSum)
            $("#register").val(data.register);
            $("#register_time").val(data.registerTime);
            $("#remark").val(data.remark);
            $("#id").val(data.id)
        }

        function findAcc() {
            $.ajax({
                type: 'post',
                url: '/SysUser/queryUser',
                success: function (result) {
                    $("#checker").val(result.loginId);
                }
            })
        }
        function changeSpayNo() {
            $.ajax({
                type:'post',
                url:'/spay/changeSpayNo',
                data: 'id='+$("#id").val(),
                success:function (result) {
                    if(result==1){
                        alert("提交成功");
                        $("#GatherTab").bootstrapTable("refresh");
                    }else{
                        alert("系统繁忙,请稍后再试")
                    }
                }
            })
        }
        function changeSpay() {
            var number3=$(".number3");
            var amont=$(".amount");
            var i=0;
            $.each(number3,function (index,value) {
                    if(number3[index].innerText<amont[index].innerText){
                        i++;

                    }
            })
            if(i==0){
                $.ajax({
                    type:'post',
                    url:'/spay/changeSpay',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify({
                        id:+$("#id").val(),
                        checkTime:$("#check_time").val(),
                        checker:$("#checker").val(),
                        storeTag:'k002-1',
                        checkTag:'s001-1',
                    }),
                    success:function (result) {
                        if(result==1){
                            alert("提交成功");
                            $("#GatherTab").bootstrapTable("refresh");
                        }else{
                            alert("系统繁忙,请稍后再试")
                        }
                    }
                })
            }else{
                alert("出库数量有误");
            }

        }
    </script>
</head>
<body>
<table id="GatherTab" class="table">

</table>


<div class="modal fade" id="myModal2">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title" style="margin-left: 43%;">出库申请单审核</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="shadow" style="width: 80%;height: 30%;margin-top: 5px;margin-left: 10%">
                    <div style="width: 90%; margin-left: 50px">

                        <input type="hidden" id="id" >
                        <div class="text-center"><h2>出库申请单</h2></div>
                        <div style="margin-top: 50px">
                            <div class="form-inline row" style="margin-left: 5%">

                                <span class="">出库人：</span>
                                <input type="text" readonly id="storer" class="form-control col-3">
                                <div class="col-2"></div>
                                <span class="">出库理由：</span>
                                <input id="reason" readonly class="form-control col-3">
                                </input>
                            </div>


                        </div>

                        <table class="table tab-content table-hover" style="margin-top: 20px;border: 1px black solid">
                            <thead class="table-info">
                            <tr class="tab-content">
                                <td>序号</td>
                                <td>产品名称</td>
                                <td>产品编号</td>
                                <td>库存数量</td>
                                <td>数量</td>
                                <td>单位</td>
                                <td>成本单价（元）</td>
                                <td>小计（元）</td>
                            </tr>
                            </thead>
                            <tbody id="tbody" class="tab-content">

                            </tbody>
                        </table>
                        <div class="form-inline row" style="margin-top: 20px;margin-left: 5% ">

                            <span class="">总数量：</span>
                            <input type="text" readonly id="amount_sum" class="form-control col-3">
                            <div class="col-2"></div>
                            <span class="">&nbsp;&nbsp;&nbsp;总金额：</span>
                            <input type="text" readonly id="cost_price_sum" class="form-control col-3">
                        </div>
                        <div class="form-inline row" style="margin-top: 20px ;margin-left: 5%">

                            <span class="">登记人：</span>
                            <input type="text" readonly id="register" class="form-control col-3">
                            <div class="col-2"></div>
                            <span class="">登记时间：</span>
                            <input id="register_time" readonly type="text" class="form-control col-3">
                        </div>
                        <div class="form-inline row" style="margin-top: 20px ;margin-left: 5%">

                            <span class="">审核人：</span>
                            <input type="text" readonly id="checker" class="form-control col-3">
                            <div class="col-2"></div>
                            <span class="">审核时间：</span>
                            <input id="check_time" readonly type="text" class="form-control col-3">
                        </div>
                        <div class="row" style="margin-top: 20px;margin-left: 20px  ">
                            <span class=" text-center">备注：</span><br>
                            <textarea readonly id="remark" style="height: 100px;width: 800px;;"></textarea>
                        </div>

                    </div>
                </div>

            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="changeSpay()">审核通过</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="changeSpayNo()">审核不通过</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>

        </div>
    </div>
</div>
</body>
</html>